<?php

namespace app\http\middleware;

use app\common\exception\FailException;
use app\common\exception\TokenException;
use app\facade\model\Member;
use app\facade\model\AdminUser;
use think\Request;

class AdminAuth
{

    public function handle(Request $request, \Closure $next)
    {
        $token = $request->header('Authorization');
        $type = $request->header('Type');
        if((!$token)) {
            throw new FailException(['msg' => '请登录']);
        }
        if($type === "admin") {
            // 检测是否存在用户
            $user = AdminUser::adminInfo();
            if (!$user) {
                throw new TokenException();
            }
            $request->user = $user;
            return $next($request);
        }else {
            // 检测是否存在用户
            $user = Member::memberInfo();
            if (!$user) {
                throw new TokenException();
            }
            $request->user = $user;
            return $next($request);
        }
    }
}